--[[
    @brief   代理设置
    @date    2019-06-25
--]]

TProxySchemeConfig = {
    PROXY_CONFIG_NONE = 1;
    PROXY_CONFIG_HTTP = 2;
}

TProxyPrefViewCtrl = 
{
    kRootPanel = "pnlProxyPref";
    kCbxProxyScheme = "pnlProxyPref\\pnlNetworkSetting\\cbxProxyScheme";
    kEdtProxyAddress = "pnlProxyPref\\pnlNetworkSetting\\pnlProxyAddrBackground\\edtProxyAddr";
    kEdtProxyUserName = "pnlProxyPref\\pnlNetworkSetting\\pnlProxyUserBackground\\edtProxyUserName";
    kEdtProxyPassword = "pnlProxyPref\\pnlNetworkSetting\\pnlProxyPwdBackground\\edtProxyPwd";
    kEdtProxyPort = "pnlProxyPref\\pnlNetworkSetting\\pnlProxyPortBackground\\edtProxyPort";
    kBtnOK = "pnlProxyPref\\pnlSettingsControl\\btnOK";
    kBtnCancel = "pnlProxyPref\\pnlSettingsControl\\btnCancel";
};


TProxyPrefView = 
{
    ParentPanelID = 0;
    
    EdtProxyAddrID = 0;
    EdtProxyUserNameID = 0;
    EdtProxyPwdID = 0;
    EdtProxyPortID = 0;

    BtnOkID = 0;
    BtnCancelID = 0;


    RootPanelPtr = nil;
    CbxProxySchemePtr = nil;

    -- 注:当前只有编辑框控件，当添加其他类型控件时需要处理DisableEditControls
    tabStopCtrlIDs = {};

    proxyConfig = {};

    InitView = function(rootID)
        TProxyPrefView.ParentPanelID = rootID;
    end;

    UnInitView = function()
    end;

    IsProxyConfigChanged = function (newConfig)
        if nil == newConfig or nil == TProxyPrefView.proxyConfig then
            return false;
        end

        local oldConfig = TProxyPrefView.proxyConfig;
        
        if newConfig.scheme_type ~= oldConfig.scheme_type then
            return true;
        end

        if newConfig.host_name ~= oldConfig.host_name then
            return true;
        end

        if newConfig.user_name ~= oldConfig.user_name then
            return true;
        end

        if newConfig.password ~= oldConfig.password then
            return true;
        end

        if newConfig.port ~= oldConfig.port then
            return true;
        end

        return false;
    end;

    CheckUI = function()
        
        TProxyPrefView.tabStopCtrlIDs = {};

        TProxyPrefView.RootPanelPtr = ui_utility.get_ctrl_ptr_by_path(TProxyPrefView.ParentPanelID,
                                                                    TProxyPrefViewCtrl.kRootPanel);
        if nil == TProxyPrefView.RootPanelPtr then
            return false;
        end

        TProxyPrefView.CbxProxySchemePtr = ui_utility.get_ctrl_ptr_by_path(
                                                                TProxyPrefView.ParentPanelID,
                                                                TProxyPrefViewCtrl.kCbxProxyScheme);
        if nil == TProxyPrefView.CbxProxySchemePtr then
            return false;
        end

        local strProxyNone, strHolder = U8toCUIStr(LoadStr("PROXY_PREF_SCHEME_NONE"));
        local strProxyHttp, strHolder = U8toCUIStr(LoadStr("PROXY_PREF_SCHEME_HTTP"));
       
        TProxyPrefView.CbxProxySchemePtr:AppendItem(strProxyNone);
        TProxyPrefView.CbxProxySchemePtr:AppendItem(strProxyHttp);

        TProxyPrefView.CbxProxySchemePtr:SetSelString(strProxyNone);

        ui_utility.setproperty_by_path(TProxyPrefView.ParentPanelID,
                                       TProxyPrefViewCtrl.kCbxProxyScheme,
                                       "OnCBSelChanged",
                                       "TProxyPrefView.OnCBProxySchemeChanged")

        TProxyPrefView.EdtProxyAddrID = ui_utility.get_ctrl_id_by_path(TProxyPrefView.ParentPanelID,
                                                               TProxyPrefViewCtrl.kEdtProxyAddress);
        if Tenio.INVALID_CONTROLID == TProxyPrefView.EdtProxyAddrID then
            return false;
        end

        table.insert(TProxyPrefView.tabStopCtrlIDs, TProxyPrefView.EdtProxyAddrID);

        TProxyPrefView.EdtProxyPortID = ui_utility.get_ctrl_id_by_path(TProxyPrefView.ParentPanelID,
                                                                TProxyPrefViewCtrl.kEdtProxyPort);
        if Tenio.INVALID_CONTROLID == TProxyPrefView.EdtProxyPortID then
            return false;
        end

        table.insert(TProxyPrefView.tabStopCtrlIDs, TProxyPrefView.EdtProxyPortID);

        TProxyPrefView.EdtProxyUserNameID = ui_utility.get_ctrl_id_by_path(
                                                            TProxyPrefView.ParentPanelID,
                                                            TProxyPrefViewCtrl.kEdtProxyUserName);
        if Tenio.INVALID_CONTROLID == TProxyPrefView.EdtProxyUserNameID then
            return false;
        end

        table.insert(TProxyPrefView.tabStopCtrlIDs, TProxyPrefView.EdtProxyUserNameID);

        TProxyPrefView.EdtProxyPwdID = ui_utility.get_ctrl_id_by_path(TProxyPrefView.ParentPanelID,
                                                            TProxyPrefViewCtrl.kEdtProxyPassword);
        if Tenio.INVALID_CONTROLID == TProxyPrefView.EdtProxyPwdID then
            return false;
        end

        table.insert(TProxyPrefView.tabStopCtrlIDs, TProxyPrefView.EdtProxyPwdID);

        TProxyPrefView.BtnOkID = ui_utility.get_ctrl_id_by_path(TProxyPrefView.ParentPanelID,
                                                                TProxyPrefViewCtrl.kBtnOK);
        if Tenio.INVALID_CONTROLID == TProxyPrefView.BtnOkID then
            return false;
        end

        ui_utility.setproperty_by_id(TProxyPrefView.BtnOkID, "OnClick", "TProxyPrefView.OnClickOK");

        TProxyPrefView.BtnCancelID = ui_utility.get_ctrl_id_by_path(TProxyPrefView.ParentPanelID,
                                                                TProxyPrefViewCtrl.kBtnCancel);
        if Tenio.INVALID_CONTROLID == TProxyPrefView.BtnCancelID then
            return false;
        end

        ui_utility.setproperty_by_id(TProxyPrefView.BtnCancelID,
                                                        "OnClick", "TProxyPrefView.OnClickCancel");

        local proxyConfig = comm_center.call_service("Srv_GetOrSetProxyInfo", { get = true });
        if nil ~= proxyConfig then
            TProxyPrefView.proxyConfig = proxyConfig;
            TProxyPrefView.UpdateControls(proxyConfig);
        end

        for idx = 1, #TProxyPrefView.tabStopCtrlIDs do
            ui_utility.setproperty_by_id(TProxyPrefView.tabStopCtrlIDs[idx],
                            "OnKeyDown", "TProxyPrefView.OnKeyDown"); 
        end;

        return true;
    end;

    ShowControl = function(ctrlPtr, show)
        if nil == ctrlPtr then
            return;
        end

        if show then
            ctrlPtr:Show(TRUE);
        else
            ctrlPtr:Show(FALSE);
        end
    end;

    ShowView = function()
        
        if TProxyPrefView.CheckUI() then
            TProxyPrefView.ShowControl(TProxyPrefView.RootPanelPtr, true);
            comm_center.call_service("Svr_ShowLoginNavTab", { show = false });
        end

    end;

    HideView = function()
       TProxyPrefView.ShowControl(TProxyPrefView.RootPanelPtr, false);
       comm_center.call_service("Svr_ShowLoginNavTab", { show = true });
    end;

    UpdateControls = function(config)
        if nil == config then
            return;
        end

        -- proxy type
        local scheme = config["scheme_type"] or 0;
        if TProxySchemeConfig.PROXY_CONFIG_HTTP == scheme then
            TProxyPrefView.CbxProxySchemePtr:SetCurSel(1);
        else
            TProxyPrefView.CbxProxySchemePtr:SetCurSel(0);
        end

        -- proxy address
        local hostName = config["host_name"] or "";
        ui_utility.setproperty_by_id(TProxyPrefView.EdtProxyAddrID, "Caption", hostName);

        -- proxy user name
        local userName = config["user_name"] or "";
        ui_utility.setproperty_by_id(TProxyPrefView.EdtProxyUserNameID, "Caption", userName);

        -- proxy password
        local password = config["password"] or "";
        ui_utility.setproperty_by_id(TProxyPrefView.EdtProxyPwdID, "Caption", password);

        local port = "";
        if nil ~= config["port"] and 0 ~= config["port"] then
            port = tostring(config["port"]);
        end

        ui_utility.setproperty_by_id(TProxyPrefView.EdtProxyPortID, "Caption", port);

        if TProxySchemeConfig.PROXY_CONFIG_HTTP == scheme then
            TProxyPrefView.DisableEditControls(false);
        else
            TProxyPrefView.DisableEditControls(true);
        end
    end;

    GetEditCtrlText = function(ctrlID, isPwd)
        local ctrlPtr = ui_utility.get_ctrl_ptr_by_id(ctrlID);
        if nil == ctrlPtr then
            return nil;
        end

        if true == isPwd then
            local pwd, holder = NewUIStr(260);
            if nil == pwd then
                return nil;
            end
            ctrlPtr:GetPassword(pwd, 260);
            return UIStrtoU8(pwd);
        else
           local text = ctrlPtr:GetText();
           if nil == text then
                return nil;
           end
           return CUIStrtoU8(text);
        end
    end;

    OnCBProxySchemeChanged = function()
        local curSel = TProxyPrefView.CbxProxySchemePtr:GetCurSel();
        if 0 == curSel then
            TProxyPrefView.UpdateControls({ scheme_type = 0 });
        else
            local proxyConfig = comm_center.call_service("Srv_GetOrSetProxyInfo", { get = true });
            proxyConfig["scheme_type"] = 2;
            TProxyPrefView.UpdateControls(proxyConfig);
        end
    end;

    OnClickOK = function()
        local proxyConfig = {};
        proxyConfig["scheme_type"] = 0;
        local schemeType = TProxyPrefView.CbxProxySchemePtr:GetCurSel();
        if 1 == schemeType then
            proxyConfig["scheme_type"] = 2;
        end

        local text = TProxyPrefView.GetEditCtrlText(TProxyPrefView.EdtProxyAddrID);
        if nil ~= text then
            proxyConfig["host_name"] = text;
        end

        text = TProxyPrefView.GetEditCtrlText(TProxyPrefView.EdtProxyUserNameID);
        if nil ~= text then
            proxyConfig["user_name"] = text;
        end

        text = TProxyPrefView.GetEditCtrlText(TProxyPrefView.EdtProxyPwdID, true);
        if nil ~= text then
            proxyConfig["password"] = text;
        end

        text = TProxyPrefView.GetEditCtrlText(TProxyPrefView.EdtProxyPortID);
        if nil ~= text and "" ~= text then
            proxyConfig["port"] = tonumber(text);
        else
            proxyConfig["port"] = 1080;
        end

        proxyConfig["get"] = false;
        comm_center.call_service("Srv_GetOrSetProxyInfo", proxyConfig);

        local newConfig = comm_center.call_service("Srv_GetOrSetProxyInfo", { get = true });
        if TProxyPrefView.IsProxyConfigChanged(newConfig) then
           local tipParam = {
                position = "center",
                title = "",
                width = 350,
                height = 230,
                titleIcon = "",
                icon = "",
                tipsIcon = "",
                descTitle = "",
                desc = LoadStr("CLIENT_SET_RESTART_TIP"),
                okBtn =  {
                    visible = "true",
                    text = LoadStr("LANGUAGE_SET_RESTART"),
                    handle = function()
                       poptips_control.close_tips();
                       comm_center.call_service("Svr_RestartWG", {});
                    end
                },
                cancelBtn =  {
                    visible = "true",
                    text = LoadStr("ID_TIPS_CANCEL"),
                    handle = function()
                        comm_center.call_service("Svr_LoginPageGoBack", { });
                    end
              }
            }
            poptips_control.show_tips(tipParam);
        else
            comm_center.call_service("Svr_LoginPageGoBack", { });
        end
    end;

    OnClickCancel = function()
        comm_center.call_service("Svr_LoginPageGoBack", { });
    end;

    OnKeyDown = function(UILib, dwEdtID, dwVirtKey, dwKeyData)
        --VK_TAB
        if 9 == dwVirtKey then
            local nextCtrlID = Tenio.INVALID_CONTROLID;
            for idx = 1, #TProxyPrefView.tabStopCtrlIDs do
                if TProxyPrefView.tabStopCtrlIDs[idx] == dwEdtID then
                    local nextIdx = (idx + 1) % (#TProxyPrefView.tabStopCtrlIDs + 1);
                    if nextIdx == 0 then
                        nextIdx = 1;
                    end
                    nextCtrlID = TProxyPrefView.tabStopCtrlIDs[nextIdx];
                end
            end;
            local ptr = ui_utility.get_ctrl_ptr_by_id(nextCtrlID);
            if nil ~= ptr then
                ptr:SetFocus();
            end
        end

        -- VK_RETURN
        if 13 == dwVirtKey and dwEdtID == TProxyPrefView.EdtProxyPortID then
            TProxyPrefView.OnClickOK();
        end
    end;

    DisableEditControls = function(enabled)
        local value = "false";
        if enabled then
            value = "true";
        end;
        local editCtrls = TProxyPrefView.tabStopCtrlIDs or {};
        for idx = 1, #editCtrls do
            ui_utility.setproperty_by_id(editCtrls[idx], "Disable", value); 
        end;
    end;

};